using System;
using System.Collections.Generic;
using System.Linq;
using Hematology.Data.Interface;

namespace Hematology.Data
{
	public partial class City : IComparable<City>, IGeoItem
	{
		public static List<City> GetAllCities(int countryId, int? regionId, bool? isOnlyVisible)
		{
			List<int> lRegionId = new List<int>();
			if (regionId == null || regionId.Value == 0)
			{
				List<Region> lRegion = Region.Search(null, countryId, null, true);
				if (lRegion != null)
				{
					for (int regionIndex = 0; regionIndex < lRegion.Count; regionIndex++)
					{
						if (lRegion[regionIndex].Id != null)
							lRegionId.Add(lRegion[regionIndex].Id.Value);
					}
				}
			}

			List<City> lCity = new List<City>();
			if (lRegionId.Count > 0)
			{
				foreach (int currentRegionId in lRegionId)
				{
					lCity.AddRange(Search(null, countryId, currentRegionId, null, isOnlyVisible));
				}
			}
			else if (regionId != null)
			{
				lCity.AddRange(Search(null, countryId, regionId, null, isOnlyVisible));
			}
			else
			{
				lCity.AddRange(Search(null, countryId, null, null, isOnlyVisible));
			}

			lCity.Sort();

			return lCity;
		}

		public static City SearchByName(string name)
		{
			return Search(null, null, null, name, null).FirstOrDefault();
		}

		public int CompareTo(City other)
		{
			return Name.CompareTo(other.Name);
		}
	}
}

